#include "Acceptor.hpp"
#include "Connection.hpp"
#include <vector>

class EpollTcpServer
{
public:
    EpollTcpServer(uint16_t port)
        :_port(port), _epoller(), \
        _acceptor(_port, nullptr, nullptr, nullptr, &_epoller), \
        _isRunning(false)
    {}

    bool Init()
    {
        // 注册信息处理方法
        _acceptor.SetOnMessage(std::bind(&EpollTcpServer::OnMseeage, this, std::placeholders::_1));
        // 初始化Acceptor
        return _acceptor.Init();
    }

    bool Start()
    {
        _acceptor.Start();
        std::vector<Connection> ready;
        _epoller.Wait(&ready);
        for (auto& conn : ready)
        {
            if (conn.Events() & EPOLLIN) {
                conn.onMessage()[];
            }
            else if (conn.Events() & EPOLLOUT) {
                // ...
            }
            else {
                // ...
            }
        }
    }

private:
    void OnMseeage(const std::string& message)
    {
        // handle message
    }

private:
    uint16_t _port;
    Epoll _epoller;
    Acceptor _acceptor;
    bool _isRunning;
};